# Maintainer: Drew Waranis <drew@waran.is>

_realname=apr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.7.4
pkgrel=1
pkgdesc="The Apache Portable Runtime (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://apr.apache.org/"
msys2_references=(
  "cpe: cpe:/a:apache:portable_runtime"
)
license=('spdx:Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-python")
source=(https://downloads.apache.org/apr/${_realname}-${pkgver}.tar.bz2{,.asc}
        0001-apr-fix-linking-error.patch
        0002-apr-remove-full-path.patch
        0003-apr-prevent-override-build-flags.patch
        0004-apr-install-missing-m4-files.patch
        'apr_ssize_t.patch'
        'apr_wtypes.patch')
sha256sums=('fc648de983f3a2a6c9e78dea1f180639bd2fad6c06d556d4367a701fe5c35577'
            'SKIP'
            'c5207578313c2c2605a4dc8a498055f847f158e126325918194c059f356c484f'
            'cc52201e09dbb080ffc259a57e1dd27267c751e2d3be8368d82adbbeb9de46c8'
            'd11b3ec7dbd71cfc2c6677dcc06f01adf9182881f3edb7f70f562a51923e8209'
            '254686b307b8c5189aa467eff159d99521571eab26864ce9da5b22a6366451c8'
            'ba7d6de7e7930801df483d444b97c159af4ff11b4ce27e1337aea5e0417e6066'
            'b82dd98ec8cff2273fb071dc9f1d2ee7466905c9b82a12d3d83ce1cb5920a5d6')
# pgp keys are listed here https://people.apache.org/keys/committer/
validpgpkeys=('65B2D44FE74BD5E3DE3AC3F082781DE46D5954FA') # Eric Covener <covener@apache.org>

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  _apply_patch_with_msg \
    0001-apr-fix-linking-error.patch \
    0002-apr-remove-full-path.patch \
    0003-apr-prevent-override-build-flags.patch \
    0004-apr-install-missing-m4-files.patch

  patch -p0 -i "${srcdir}"/apr_ssize_t.patch
  patch -p0 -i "${srcdir}"/apr_wtypes.patch

  ./buildconf
  # autoreconf -fi
}

build() {
  mkdir -p "build-${MSYSTEM}" && cd "build-${MSYSTEM}"

  # Disable IPv6.
  ../${_realname}-${pkgver}/configure \
    --prefix="${MINGW_PREFIX}" \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-static \
    --enable-shared \
    --includedir="${MINGW_PREFIX}/include/apr-1" \
    --with-installbuilddir="${MINGW_PREFIX}/share/apr-1/build" \
    --enable-nonportable-atomics \
    --with-devrandom=/dev/urandom \
    --disable-ipv6

  make
}

#check() {
#  cd "${srcdir}/build-${MSYSTEM}"
#  make -j1 check
#}

package() {
  cd "build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install

  # remove full path reference
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}"/share/apr-1/build/libtool
}
